//
//  ZZHomeTableViewCell.swift
//  web
//
//  Created by 张卓 on 2017/2/4.
//  Copyright © 2017年 ZZ. All rights reserved.
//

import UIKit

class ZZHomeTableViewCell: UITableViewCell {
    
    // 记录约束
    var RetweetViewConstraint : Constraint?
    
    
    //提供属性共外界赋值
    var statusViewModel:ZZStatusViewModel?{
        didSet{
            //为原创微博赋值
            originalView.statuViewModel = statusViewModel
            //为转发微博赋值
//            retweetView.statusViewModel = statusViewModel
            
            //删除约束记录
            RetweetViewConstraint?.uninstall()
            
            //判断是否有转发微博
//            print(statusViewModel?.homeModel?.retweeted_status)
            //有值,或者为nil
            //if判断是否为nil
            if statusViewModel?.homeModel?.retweeted_status == nil{
                //没有值
                //设置约束
                bottomView.snp_makeConstraints(closure: { (make) in
                    RetweetViewConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                //隐藏转发微博
                retweetView.isHidden = true
            }else{
                //有值
                //赋值
                retweetView.statusViewModel = statusViewModel
                //设置约束
                bottomView.snp_makeConstraints(closure: { (make) in
                    RetweetViewConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                //显示转发微博
                retweetView.isHidden = false
            }
            
            
            
        }
    }


    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        //设置背景颜色
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        //添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        //设置约束
        originalView.snp_makeConstraints { (make) in
            make.left.right.equalTo(self)
            make.top.equalTo(8)
        }
        
        retweetView.snp_makeConstraints { (make) in
            // 转发微博的高度需要在转发微博内部计算出来
            //            make.height.equalTo(50)
            make.left.right.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
        }
        
        bottomView.snp_makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            //记录顶部约束
            RetweetViewConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(35)
            make.bottom.equalTo(contentView)
        }
        
    }
    // 原创微博
    private lazy var originalView:ZZHomeOriginalView = ZZHomeOriginalView()
    // 转发微博
    private lazy var retweetView: ZZHomeRetweetView = ZZHomeRetweetView()
    // 底部视图
    private lazy var bottomView: ZZHomeBottomView = {
       let view = ZZHomeBottomView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
}
